package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.IntStream;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PointsRuleArtificial;
import com.ruoyi.system.domain.PointsRuleSystem;
import com.ruoyi.system.service.IPointsRuleArtificialService;
import com.ruoyi.system.service.IPointsRuleSystemService;
import org.apache.regexp.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PointsRuleMapper;
import com.ruoyi.system.domain.PointsRule;
import com.ruoyi.system.service.IPointsRuleService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
 * 积分规则Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@Service
public class PointsRuleServiceImpl implements IPointsRuleService
{
    @Autowired
    private PointsRuleMapper pointsRuleMapper;
    @Autowired
    private IPointsRuleSystemService pointsRuleSystemService;
    @Autowired
    private IPointsRuleArtificialService pointsRuleArtificialService;

    /**
     * 查询积分规则
     *
     * @param id 积分规则主键
     * @return 积分规则
     */
    @Override
    public PointsRule selectPointsRuleById(Long id)
    {
        PointsRule pointsRule = pointsRuleMapper.selectPointsRuleById(id);
        PointsRuleSystem pointsRuleSystem = new PointsRuleSystem();
        pointsRuleSystem.setPointsRuleId(id);
        List<PointsRuleSystem> pointsRuleSystems = pointsRuleSystemService.selectPointsRuleSystemList(pointsRuleSystem);
        PointsRuleArtificial pointsRuleArtificial = new PointsRuleArtificial();
        pointsRuleArtificial.setPointsRuleId(id);
        List<PointsRuleArtificial> pointsRuleArtificials = pointsRuleArtificialService.selectPointsRuleArtificialList(pointsRuleArtificial);
        pointsRule.setPointsRuleSystemList(pointsRuleSystems);
        pointsRule.setPointsRuleArtificialList(pointsRuleArtificials);
        return pointsRule;
    }

    /**
     * 查询积分规则
     *
     * @param pointsRule 积分规则主键
     * @return 积分规则
     */
    @Override
    public PointsRule selectPointsRuleByPointsRule(PointsRule pointsRule){
        return pointsRuleMapper.selectPointsRuleByPointsRule(pointsRule);
    }

    /**
     * 查询积分规则列表
     *
     * @param pointsRule 积分规则
     * @return 积分规则
     */
    @Override
    public List<PointsRule> selectPointsRuleList(PointsRule pointsRule)
    {
        List<PointsRule> pointsRules = pointsRuleMapper.selectPointsRuleList(pointsRule);
        IntStream.range(0,pointsRules.size()).forEach(
                i->{
                   int index = i+1;
                    PointsRule pointsRuleVo = pointsRules.get(i);
                    pointsRuleVo.setOrderNum(index);
                });
        return pointsRules;
    }

    /**
     * 新增积分规则
     *
     * @param pointsRule 积分规则
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertPointsRule(PointsRule pointsRule)
    {
        pointsRule.setCreateTime(DateUtils.getNowDate());
        String effectiveTerm = pointsRule.getEffectiveTerm();
        PointsRule pointsRuleByEffectiveTerm = pointsRuleMapper.selectPointsRuleByEffectiveTerm(effectiveTerm);
        if(!ObjectUtils.isEmpty(pointsRuleByEffectiveTerm)){
            return AjaxResult.error("已经有"+effectiveTerm+"有效期内的规则,请勿重复添加");
        }
        //添加当前年份之后的规则 状态待生效并只能有一条
        Integer nowYear = Integer.valueOf(DateUtils.getNowYear());
        if(Integer.valueOf(effectiveTerm)>nowYear){
            PointsRule pointsRuleVo = new PointsRule();
            pointsRuleVo.setStatus("2");
            pointsRuleVo.setEffectiveTerm(effectiveTerm);
            PointsRule pointsRule1 = pointsRuleMapper.selectPointsRuleByPointsRule(pointsRuleVo);
            if(!ObjectUtils.isEmpty(pointsRule1)){
                return AjaxResult.error("已经有"+effectiveTerm+"待生效的规则,请勿重复添加");
            }
            pointsRule.setStatus("2");
        }
        if(Integer.valueOf(effectiveTerm)<nowYear){
            PointsRule pointsRuleVo = new PointsRule();
            pointsRuleVo.setStatus("1");
            pointsRuleVo.setEffectiveTerm(effectiveTerm);
            PointsRule pointsRule1 = pointsRuleMapper.selectPointsRuleByPointsRule(pointsRuleVo);
            if(!ObjectUtils.isEmpty(pointsRule1)){
                return AjaxResult.error("已经有"+effectiveTerm+"过期的规则,请勿重复添加");
            }
            pointsRule.setStatus("1");
        }
        try {
            pointsRuleMapper.insertPointsRule(pointsRule);
            List<PointsRuleArtificial> pointsRuleArtificialList = pointsRule.getPointsRuleArtificialList();
            if(!CollectionUtils.isEmpty(pointsRuleArtificialList)){
                for(PointsRuleArtificial ruleArtificial : pointsRuleArtificialList){
                    ruleArtificial.setPointsRuleId(pointsRule.getId());
                    pointsRuleArtificialService.insertPointsRuleArtificial(ruleArtificial);
                }
            }
            List<PointsRuleSystem> pointsRuleSystemList = pointsRule.getPointsRuleSystemList();
            if(!CollectionUtils.isEmpty(pointsRuleSystemList)){
                for(PointsRuleSystem ruleSystem: pointsRuleSystemList){
                    ruleSystem.setPointsRuleId(pointsRule.getId());
                    pointsRuleSystemService.insertPointsRuleSystem(ruleSystem);
                }
            }
            return AjaxResult.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success("添加失败");
        }
    }

    /**
     * 修改积分规则
     *
     * @param pointsRule 积分规则
     * @return 结果
     */
    @Override
    public AjaxResult updatePointsRule(PointsRule pointsRule)
    {
        pointsRule.setUpdateTime(DateUtils.getNowDate());
        String effectiveTerm = pointsRule.getEffectiveTerm();
        if(StringUtils.isNotBlank(effectiveTerm)){
            PointsRule pointsRuleByEffectiveTerm = pointsRuleMapper.selectPointsRuleByEffectiveTerm(effectiveTerm);
            PointsRule pointsRuleById = pointsRuleMapper.selectPointsRuleById(pointsRule.getId());
            if(!ObjectUtils.isEmpty(pointsRuleByEffectiveTerm)&&!effectiveTerm.equals(pointsRuleById.getEffectiveTerm())){
                return AjaxResult.error("已经有"+effectiveTerm+"有效期内的规则,请勿重复添加");
            }
            //添加当前年份之后的规则 状态待生效并只能有一条
            Integer nowYear = Integer.valueOf(DateUtils.getNowYear());
            if(Integer.valueOf(effectiveTerm)>nowYear){
                PointsRule pointsRuleVo = new PointsRule();
                pointsRuleVo.setStatus("2");
                pointsRuleVo.setEffectiveTerm(effectiveTerm);
                PointsRule pointsRule1 = pointsRuleMapper.selectPointsRuleByPointsRule(pointsRuleVo);
                if(!ObjectUtils.isEmpty(pointsRule1)){
                    return AjaxResult.error("已经有"+effectiveTerm+"待生效的规则,请勿重复添加");
                }
                pointsRule.setStatus("2");
            }
            if(Integer.valueOf(effectiveTerm)<nowYear){
                PointsRule pointsRuleVo = new PointsRule();
                pointsRuleVo.setStatus("1");
                pointsRuleVo.setEffectiveTerm(effectiveTerm);
                PointsRule pointsRule1 = pointsRuleMapper.selectPointsRuleByPointsRule(pointsRuleVo);
                if(!ObjectUtils.isEmpty(pointsRule1)){
                    return AjaxResult.error("已经有"+effectiveTerm+"过期的规则,请勿重复添加");
                }
                pointsRule.setStatus("1");
            }
        }
        try {
            pointsRuleMapper.updatePointsRule(pointsRule);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }


    /**
     * 删除积分规则信息
     *
     * @param id 积分规则主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletePointsRuleById(Long id)
    {
        int i = pointsRuleMapper.deletePointsRuleById(id);
        PointsRuleSystem pointsRuleSystem = new PointsRuleSystem();
        pointsRuleSystem.setPointsRuleId(id);
        List<PointsRuleSystem> pointsRuleSystems = pointsRuleSystemService.selectPointsRuleSystemList(pointsRuleSystem);
        for(PointsRuleSystem ruleSystem : pointsRuleSystems){
            pointsRuleSystemService.deletePointsRuleSystemById(ruleSystem.getId());
        }
        PointsRuleArtificial pointsRuleArtificial = new PointsRuleArtificial();
        pointsRuleArtificial.setPointsRuleId(id);
        List<PointsRuleArtificial> pointsRuleArtificials = pointsRuleArtificialService.selectPointsRuleArtificialList(pointsRuleArtificial);
        for(PointsRuleArtificial ruleArtificial : pointsRuleArtificials){
            pointsRuleArtificialService.deletePointsRuleArtificialById(ruleArtificial.getId());
        }
        return i;
    }
}
